using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using ibba.src.noriskbo.code.behavior;
using Infragistics.Controls.Grids;

namespace ibba.src.noriskbo
{
    public partial class XamGridToolBar : UserControl
    {
        private class PageSizeItem
        {
            public string Description { get; set; }
            public int Size { get; set; }
        }

        #region private_variables
		
        private BitmapImage imgChecked;
        private BitmapImage imgUnchecked;
         
	    #endregion

        #region public_dependencyProperty
        
        //public static readonly DependencyProperty SourceBehaviorProperty = DependencyProperty.Register("SourceBehavior", typeof(XamGridBehavior), typeof(XamGridToolBar), null);
        public static readonly DependencyProperty IsExpandOnlyOneProperty = DependencyProperty.Register("IsExpandOnlyOne", typeof(bool), typeof(XamGridToolBar), null);
        public static readonly DependencyProperty IsHeaderCopyToClibboardProperty = DependencyProperty.Register("IsHeaderCopyToClibboard", typeof(bool), typeof(XamGridToolBar), null);
       
        public XamGridBehavior SourceBehavior { get; set; }
        //public XamGridBehavior SourceBehavior
        //{
        //    get { return (XamGridBehavior)GetValue(SourceBehaviorProperty); }
        //    set { SetValue(SourceBehaviorProperty, value); }
        //}
        
        public bool IsExpandOnlyOne
        {
            get { return (bool)GetValue(IsExpandOnlyOneProperty); }
            set
            {
                SetValue(IsExpandOnlyOneProperty, value);

                chkExpandOnly.Source = (value.Equals(true) ? imgChecked : imgUnchecked);
                this.SourceBehavior.IsExpandOnlyOne = value;                
            }
        }
       
        public bool IsHeaderCopyToClibboard 
        {
            get { return (bool)GetValue(IsHeaderCopyToClibboardProperty); }
            set
            {
                SetValue(IsHeaderCopyToClibboardProperty, value);

                chkCopyPaste.Source = (value.Equals(true) ? imgChecked : imgUnchecked);
                SourceBehavior.IsHeaderCopyToClibboard = value;                
            } 
        }

        #endregion

        #region contructor

        public XamGridToolBar()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(XamGridToolBar_Loaded);
        }

        void XamGridToolBar_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.IsDesignTime)
            {
                imgChecked = new BitmapImage(new Uri(Constantes.SourceImageApp.CHECKED_MINI, UriKind.Relative));
                imgUnchecked = new BitmapImage(new Uri(Constantes.SourceImageApp.UNCHECKED_MINI, UriKind.Relative));

                //LoadPagingSettings();
            }
        }
        
        #endregion
        
        #region private_methods
		
        private void LoadPagingSettings()
        {
            PagingPanel.Visibility = SourceBehavior.Source.PagerSettings.AllowPaging != PagingLocation.None ? Visibility.Visible : Visibility.Collapsed;

            if (PagingPanel.Visibility == Visibility.Visible)
                LoadPageSizeConfiguration();
        }

        private void LoadPageSizeConfiguration()
        {
            PageSize.Items.Clear();
            PageSize.Items.Add(new PageSizeItem() { Description= "Todos", Size = 0 });

            if (SourceBehavior.Source.Rows.Count > 50)
            {
                if (SourceBehavior.Source.Rows.Count > 1000) PageSize.Items.Insert(0, new PageSizeItem() { Description = "1000", Size = 1000 });
                if (SourceBehavior.Source.Rows.Count > 500) PageSize.Items.Insert(0, new PageSizeItem() { Description = "500", Size = 500 });
                if (SourceBehavior.Source.Rows.Count > 100) PageSize.Items.Insert(0, new PageSizeItem() { Description = "100", Size = 100 });

                PageSize.Items.Insert(0, new PageSizeItem() { Description = "50", Size = 50 });
            }

            PageSize.SelectedIndex = 0;
            PageSize.IsEnabled = PageSize.Items.Count > 1 ? true : false;
        }
        
        private void PageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PageSize.SelectedIndex != -1)
            {
                PageSizeItem item = PageSize.Items[PageSize.SelectedIndex] as PageSizeItem;

                if (item.Size != 0)
                {
                    SourceBehavior.Source.PagerSettings.AllowPaging = PagingLocation.Bottom;
                    SourceBehavior.Source.PagerSettings.CurrentPageIndex = 0;
                    SourceBehavior.Source.PagerSettings.PageSize = item.Size;
                }
                else
                {
                    SourceBehavior.Source.PagerSettings.AllowPaging = PagingLocation.None;
                }
            }
        }


        private void ExibirAgrupamentoButton_Click(object sender, RoutedEventArgs e)
        {
            SourceBehavior.Source.GroupBySettings.IsGroupByAreaExpanded = SourceBehavior.Source.GroupBySettings.IsGroupByAreaExpanded.Equals(true) ? false : true;
        }
               
        private void ExibirExportacaoExcelButton_Click(object sender, RoutedEventArgs e)
        {
            XamGridHelper.Exportar(SourceBehavior.Source, false, true); //.ExportToXLS(SourceBehavior.Source);           
        }

        private void PersonalizarModoExibicaoButton_Click(object sender, RoutedEventArgs e)
        {
            SourceBehavior.Source.ShowColumnChooser();
        }
        
        private void ReiniciarConfiguracaoButton_Click(object sender, RoutedEventArgs e)
        {
            SourceBehavior.ReloadColumnConfiguration();            
        }

        private void chkCopyPaste_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.IsHeaderCopyToClibboard = !this.IsHeaderCopyToClibboard;
        }

        private void chkExpandOnly_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.IsExpandOnlyOne = !this.IsExpandOnlyOne;
        }
 
	    #endregion    

        public void ShowRecordCount(int pRecordCount)
        {
            lblQuantidadeDeRegistros.Text = pRecordCount.ToString();
            panelQtdRegistro.Visibility = Visibility.Visible;
        }
    }
}
